package main

import (
	"encoding/json"
	"fmt"
	"go.manyji.com/lib/util/convert"
)

func GetUserInfo(deviceId string) (*User, error) {

	repMap := map[string]interface{}{}
	// 获取用户信息
	userInfo, err := getUserInfo(deviceId)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal([]byte(userInfo), &repMap)
	if err != nil {
		return nil, err
	}

	dataMap, ok := repMap["Data"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}
	userData, ok := dataMap["user"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}

	u := &User{
		ID:                  int64(convert.Atoi(convert.ToString(userData["id"]))),
		Name:                convert.ToString(userData["name"]),
		Avatar:              convert.ToString(userData["avatar"]),
		Email:               convert.ToString(userData["email"]),
		Coins:               int64(convert.Atoi(convert.ToString(userData["coins"]))),
		Bonus:               int64(convert.Atoi(convert.ToString(userData["bonus"]))),
		DeviceId:            convert.ToString(userData["device_id"]),
		LastLogin:           int64(convert.Atoi(convert.ToString(userData["last_login"]))),
		CoinsConsume:        int64(convert.Atoi(convert.ToString(userData["coins_consume"]))),
		BonusConsume:        int64(convert.Atoi(convert.ToString(userData["bonus_consume"]))),
		PaypalEmail:         convert.ToString(userData["paypal_email"]),
		PaypalPhone:         convert.ToString(userData["paypal_phone"]),
		VerificationCode:    convert.ToString(userData["verification_code"]),
		VerificationExpired: int64(convert.Atoi(convert.ToString(userData["verification_expired"]))),
		Status:              int64(convert.Atoi(convert.ToString(userData["status"]))),
		CreateAt:            int64(convert.Atoi(convert.ToString(userData["create_at"]))),
		UpdatedAt:           int64(convert.Atoi(convert.ToString(userData["updated_at"]))),
		Deleted:             int64(convert.Atoi(convert.ToString(userData["deleted"]))),
	}

	return u, nil
}

//func AndroidLogin(deviceId string) (*LoginRsp, error) {
//
//	callApi("Login", body, header)
//}
//
//func H5Login(deviceId string) (*User, error) {
//
//}

func FirstVisit(deviceId string) error {

	_, err := firstView(deviceId)
	return err
}
